package at.kaok.betfair;

import at.kaok.betfair.valueobjects.MarketPrice;
import betfair.exchange.APIRequestHeader;
import betfair.exchange.ArrayOfRunnerPrices;
import betfair.exchange.BFExchangeService;
import betfair.exchange.BFExchangeService_Service;
import betfair.exchange.GetMarketPricesReq;
import betfair.exchange.GetMarketPricesResp;
import betfair.exchange.MarketPrices;
import betfair.exchange.RunnerPrices;
import betfair.global.BFGlobalService;
import betfair.global.BFGlobalService_Service;

public class Exchange {

	
	public static void getMarketPrices(String session,int marketId){
		BFExchangeService service = getService();
		GetMarketPricesReq request = new GetMarketPricesReq();
		request.setHeader(getHeaderWithSession(session));
		request.setMarketId(marketId);
		GetMarketPricesResp resp = service.getMarketPrices(request);
		
		ArrayOfRunnerPrices pr = resp.getMarketPrices().getRunnerPrices();

		String s ="";
		for(RunnerPrices ps : pr.getRunnerPrices()){
			
			s+=ps.getBestPricesToBack().getPrice().get(0).getPrice()+";";
			s+=ps.getBestPricesToLay().getPrice().get(0).getPrice()+";";

		}
		MarketPrice m = new MarketPrice(marketId);
		//1 Mannschaft Back
		m.setPriceBack1(pr.getRunnerPrices().get(0).getBestPricesToBack().getPrice().get(0).getPrice());
		//1 Mannschaft Lay
		m.setPriceLay1(pr.getRunnerPrices().get(0).getBestPricesToLay().getPrice().get(0).getPrice());
		//2 Mannschaft Back
		m.setPriceBack2(pr.getRunnerPrices().get(1).getBestPricesToBack().getPrice().get(0).getPrice());
		//2 Mannschaft Lay
		m.setPriceLay2(pr.getRunnerPrices().get(1).getBestPricesToLay().getPrice().get(0).getPrice());
		//X Back
		m.setPriceBackX(pr.getRunnerPrices().get(2).getBestPricesToBack().getPrice().get(0).getPrice());
		//X Lay
		m.setPriceLayX(pr.getRunnerPrices().get(2).getBestPricesToLay().getPrice().get(0).getPrice());
		
		
			
				
		
	}
	
	private static APIRequestHeader getHeaderWithSession(String session){
		APIRequestHeader header = new APIRequestHeader();
		header.setSessionToken(session);
		return header;
		
	}
	private static BFExchangeService getService(){
		BFExchangeService_Service sr = new BFExchangeService_Service();
		BFExchangeService it = sr.getPort(BFExchangeService.class);
		return it;
		
	}
}
